package com.situ.mall.product.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.order.OrderItem;
import com.situ.mall.pojo.entity.product.Product;
import com.situ.mall.product.pojo.query.ProductQuery;
import com.situ.mall.product.pojo.vo.ProductCountVO;
import com.situ.mall.product.pojo.vo.ProductVO;
import com.situ.mall.product.service.IProductService;
import jakarta.servlet.http.HttpServletResponse;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private IProductService productService;
	
	//商品列表
	@GetMapping("/list")
	public Result<IPage<ProductVO>> list(ProductQuery productQuery) {
		IPage<ProductVO> listPage = productService.selectList(productQuery);
		/*IPage<Map<String,Object>> listPage = productService.selectList(productQuery);
		Map<String, Object> map = new HashMap<>();
		map.put("total", listPage.getTotal());
		System.out.println("total:" + listPage.getTotal());
		System.out.println("listPage.getRecords():" + listPage.getRecords());
		map.put("list", listPage.getRecords());*/
		return Result.ok(listPage);
	}
	
	@GetMapping("/selectPromoProduct")
	public Result<List<Product>> selectPromoProduct(String categoryName){
		List<Product> list=productService.selectPromoProduct(categoryName);
		return Result.ok(list);
	}
	
	
	@GetMapping("/getHotProduct")
	public Result<List<Product>> getHotProduct(String categoryName){
		List<String> categoryList= List.of(categoryName.split(","));
		List<Product> list=productService.getHotProduct(categoryList);
		return Result.ok(list);
	}
	
	//【后台管理】——批量删除
	@DeleteMapping("/deleteAll/{ids}")
	public Result<Object> deleteAll(@PathVariable Integer[] ids) {
		productService.removeByIds(Arrays.asList(ids));
		return Result.ok("删除成功！");
	}
	
	//【后台管理】——单个删除
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Integer id) {
		productService.removeById(id);
		return Result.ok("删除成功！");
	}
	
	//【后台管理】——添加商品
	@PostMapping("/add")
	public Result add(@RequestBody Product product) {
		productService.save(product);
		return Result.ok("添加成功！");
	}
	
	//查找商品
	@GetMapping("/selectById/{id}")
	public Result<Product> selectById(@PathVariable Long id) {
		Product product1=productService.selectById(id);
		return Result.ok(product1);
	}
	
	//【后台管理】——修改商品信息
	@PutMapping("/update")
	public Result update(@RequestBody Product product) {
		productService.update(product);
		//productService.saveOrUpdate(product);
		return Result.ok("修改成功！");
	}
	
	//【后台管理】——修改商品状态
	@PutMapping("/updateStatus/{id}/{status}")
	public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
		UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id",id).set("status",status);
		productService.update(updateWrapper);
		return Result.ok("修改成功！");
	}
	
	//【后台管理】——导出Excel
	@GetMapping("/exportExcel")
	public void exportExcel(HttpServletResponse response){
		productService.exportExcel(response);
	}
	
	//【后台管理】——导入Excel
	@RequestMapping("/importExcel")
	public Result importExcel(MultipartFile file){
		productService.importExcel(file);
		return Result.ok();
	}
	
	@PutMapping("/payOrder")
	public Result payOrder(@RequestParam List<OrderItem> orderItemList){
		productService.payment(orderItemList);
		return Result.ok("付款成功！");
	}
	
	@GetMapping("/selectHotProduct")
	public Result<List<ProductCountVO>> selectHotProduct(){
		List<ProductCountVO> list=productService.selectHotProduct();
		return Result.ok(list);
	}
	
	@Autowired
	private RedissonClient redissonClient;
	
	@GetMapping("/hello")
	public String hello(){
		//获取一把锁
		RLock lock=redissonClient.getLock("my-lock");
		//加锁
		lock.lock();
		try {
			System.out.println("加锁成功！执行业务……"+Thread.currentThread().getName());
			try {
				TimeUnit.SECONDS.sleep(70);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
			//......
		}finally {
			//解锁
			System.out.println("释放锁......."+Thread.currentThread().getName());
			lock.unlock();
		}
		return "hello";
	}
}

